1 Set working dir

## [1] "Current working directory: C:/Users/flyku/Documents/GitHub/BMBL-analysis-notebooks/figure_code"

2 One group UMAP

DimPlot(combined,
        reduction = "umap",
        group.by = "seurat_clusters",
        cols = cell_type_color,
        label = T,
        pt.size = 0.8)

2.1 Save to PNG

Idents(combined) <- combined$seurat_clusters

p0 <- DimPlot(
  combined,
  reduction = "umap",
  cols = cell_type_color,
  label = F,
  pt.size = 0.4,
  repel = T,
  label.box = F
) + theme(
  axis.line = element_blank(),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks = element_blank(),
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  panel.background = element_blank(),
  panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  plot.background = element_blank()
)

png(
  paste0('./img/cluster_umap.png'),
  width = 2500,
  height = 1600,
  res = 300
)
print(p0)
dev.off()
## png 
##   2

3 UMAP split by groups

DimPlot(combined,
        reduction = "umap",
        group.by = "cell_type",
        split.by = "group",
        cols = cell_type_color,
        label = T,
        pt.size = 0.8)

3.1 Save to PNG

## png 
##   2

4 UMAP within circos plot

This circlize plot was inspired by the data visualization in a published paper (Figure1, https://www.nature.com/articles/s41586-021-03775-x) from Linnarsson’s lab.

nature example fig1

4.1 How to install

bioc_packages <- c("biomaRt", "GenomeInfoDb","EnsDb.Hsapiens.v86","GEOquery","simplifyEnrichment","ComplexHeatmap")

check_plot1cell <-
  !("plot1cell" %in% installed.packages()[, "Package"])
if (check_plot1cell) {
  bioc_packages <-
    c(
      "biomaRt",
      "GenomeInfoDb",
      "EnsDb.Hsapiens.v86",
      "GEOquery",
      "simplifyEnrichment",
      "ComplexHeatmap"
    )
  dev_packages <- c("DoubletFinder", "hdf5r", "loomR")
  dev.packages <-
    c("chris-mcginnis-ucsf/DoubletFinder",
      "Novartis/hdf5r",
      "mojaveazure/loomR")
  bioc_np <-
    bioc_packages[!(bioc_packages %in% installed.packages()[, "Package"])]
  dev_np <-
    dev_packages[!(dev_packages %in% installed.packages()[, "Package"])]
  
  if (!require("BiocManager"))
    install.packages("BiocManager")
  if (length(bioc_np)) {
    BiocManager::install(bioc_np)
  }
  if (length(dev_np)) {
    devtools::install_github(dev.packages[!(dev_packages %in% installed.packages()[, "Package"])])
  }
  devtools::install_github("TheHumphreysLab/plot1cell")
}
plot_circlize <-
  function (data_plot,
            do.label = T,
            contour.levels = c(0.2, 0.3),
            pt.size = 0.5,
            kde2d.n = 1000,
            contour.nlevels = 100,
            bg.color = "#F9F2E4",
            col.use = NULL,
            label.cex = 0.5,
            repel = FALSE)
  {
    centers <-
      data_plot %>% dplyr::group_by(Cluster) %>% summarise(x = median(x = x),
                                                           y = median(x = y))
    z <- MASS::kde2d(data_plot$x, data_plot$y, n = kde2d.n)
    celltypes <- names(table(data_plot$Cluster))
    cell_colors <- (scales::hue_pal())(length(celltypes))
    if (!is.null(col.use)) {
      cell_colors = col.use
      col_df <- data.frame(Cluster = celltypes, color2 = col.use)
      cells_order <- rownames(data_plot)
      data_plot <- merge(data_plot, col_df, by = "Cluster")
      rownames(data_plot) <- data_plot$cells
      data_plot <- data_plot[cells_order,]
      data_plot$Colors <- data_plot$color2
    }
    circos.clear()
    par(bg = bg.color)
    circos.par(
      cell.padding = c(0, 0, 0, 0),
      track.margin = c(0.01,
                       0),
      track.height = 0.01,
      gap.degree = c(rep(2, (
        length(celltypes) -
          1
      )), 12),
      points.overflow.warning = FALSE
    )
    circos.initialize(sectors = data_plot$Cluster, x = data_plot$x_polar2)
    circos.track(
      data_plot$Cluster,
      data_plot$x_polar2,
      y = data_plot$dim2,
      bg.border = NA,
      panel.fun = function(x, y) {
        circos.text(
          CELL_META$xcenter,
          CELL_META$cell.ylim[2] +
            mm_y(4),
          CELL_META$sector.index,
          cex = 0.7,
          col = "black",
          facing = "bending.inside",
          niceFacing = T,
        )
        circos.axis(labels.cex = 0.4,
                    col = "black",
                    labels.col = "black")
      }
    )
    for (i in 1:length(celltypes)) {
      dd <- data_plot[data_plot$Cluster == celltypes[i],]
      circos.segments(
        x0 = min(dd$x_polar2),
        y0 = 0,
        x1 = max(dd$x_polar2),
        y1 = 0,
        col = cell_colors[i],
        lwd = 3,
        sector.index = celltypes[i]
      )
    }
    text(
      x = 1,
      y = 0.1,
      labels = "Cell type",
      cex = 0.7,
      col = "black",
      srt = -90
    )
    points(
      data_plot$x,
      data_plot$y,
      pch = 19,
      col = alpha(data_plot$Colors,
                  0.2),
      cex = pt.size
    )
    contour(
      z,
      drawlabels = F,
      nlevels = 100,
      levels = contour.levels,
      col = "#ae9c76",
      add = TRUE
    )
    if (do.label) {
      if (repel) {
        textplot(
          x = centers$x,
          y = centers$y,
          words = centers$Cluster,
          cex = label.cex,
          new = F,
          show.lines = F
        )
      }
      else {
        text(
          centers$x,
          centers$y,
          labels = centers$Cluster,
          cex = label.cex,
          col = "black"
        )
      }
    }
  }
library(plot1cell)

###Prepare data for ploting
Idents(combined) <- combined$cell_type

circ_data <- prepare_circlize_data(combined, scale = 0.75)
color1<-rand_color(length(names(table(combined$group))))
color2<-rand_color(length(names(table(combined$sex))))

data_plot <- circ_data
###plot and save figures
png(
  filename =  './img/circlize_umap_plot.png',
  width = 1800,
  height = 1800,
  res = 300
)
plot_circlize(
  circ_data,
  contour.levels = c(0.5),
  contour.nlevels = 1000,
  do.label = T,
  pt.size = 0.5,
  col.use = cell_type_color[seq_len(length(unique(Idents(combined))))],
  bg.color = 'white',
  kde2d.n = 1000,
  repel = T,
  label.cex = 0.7
)

add_track(circ_data,
          group = "group",
          colors = color1,
          track_num = 2) ## can change it to one of the columns in the meta data of your seurat object
add_track(circ_data,
          group = "sex",
          colors = color2,
          track_num = 3) ## can change it to one of the columns in the meta data of your seurat object

dev.off()
## png 
##   2

circos umap